package cn.zzszxyy.hr.controller;

import cn.zzszxyy.hr.comm.Global;
import cn.zzszxyy.hr.model.DepartmentEntity;
import cn.zzszxyy.hr.model.UserEntity;
import cn.zzszxyy.hr.service.DeptService;
import cn.zzszxyy.hr.support.AuthPassport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ExtendedModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 作者:张伟
 * <p>
 * 联系:adamzzww@163.com
 * 时间:2016-11-28
 * 说明:无
 */
@Controller
@RequestMapping(Global.PATH)
public class DeptController extends ControllerEx {

    private final String path = "/dept";

    @Autowired
    private DeptService deptService;

    @AuthPassport
    @RequestMapping(value = path + "/list", method = RequestMethod.GET)
    public String get(HttpServletRequest request, ExtendedModelMap map) {
        Integer pageIndex = getPageIndex(request);
        Page<DepartmentEntity> page = deptService.findAllWithPage(pageIndex, Global.PAGE_SIZE);
        map.put("page", page);
        map.put("pageIndex", pageIndex);

        return Global.DESKTOP + path + "/dept-list";
    }

    //@AuthPassport
    @RequestMapping(value = path + "/{id}", method = RequestMethod.GET)
    public String get(@PathVariable int id, ExtendedModelMap map) {
        if (id >= 1) {
            DepartmentEntity deptEntity = deptService.findOne(id);
            map.put("deptEntity", deptEntity);
        }
        List<DepartmentEntity> deptList = deptService.findAll();
        map.put("deptList", deptList);
        return Global.DESKTOP + path + "/dept-edit";
    }

    @RequestMapping(value = path + "/add", method = RequestMethod.GET)
    public String add(ExtendedModelMap map) {
        List<DepartmentEntity> deptList = deptService.findAll();
        map.put("deptList", deptList);
        return Global.DESKTOP + path + "/dept-edit";
    }

    @ResponseBody
    @RequestMapping(value = path, method = {RequestMethod.PUT, RequestMethod.POST})
    public String post(HttpServletRequest request, DepartmentEntity deptEntity) {
        UserEntity user = (UserEntity) request.getSession().getAttribute(Global.COOKIE_USER_OBJECT);
        deptEntity.setCreateTime(new Date());
        deptEntity.setCreator(user.getUserName());
        if (deptService.save(deptEntity) != null) {
            return Global.RE_SUCCESS;
        } else {
            return Global.RE_ERROR;
        }
    }

    @ResponseBody
    @RequestMapping(value = path + "/{id}", method = RequestMethod.DELETE)
    public String delete(@PathVariable int id) {
        deptService.delete(id);
        return Global.RE_SUCCESS;//"redirect:" + Global.Web.PATH + path;
    }
}
